#include <iostream>
#include <pthread.h>
#include <vector>
#include <unistd.h>
#include <cassert>
using namespace std;
#define NUM 1024
struct thread
{
    pthread_t tid; // 线程ID
    char namebuffer[NUM];
};
void *start_routine(void *arg)
{
    const char *str = static_cast<char *>(arg);
    cout << str << endl;
    return nullptr; // 如有需要可返回目标数据
}
int main()
{
    vector<thread *> threads;
    for (int i = 1; i <= 10; ++i)
    {
        thread *th = new thread();
        snprintf(th->namebuffer, sizeof(th->namebuffer), "新建了第%d个线程", i);
        int ret = pthread_create(&th->tid, nullptr, start_routine, th->namebuffer);
        assert(ret == 0);
        threads.push_back(th);
        sleep(1);
    }
    // 进程线程操作
    //...
    for (auto &it : threads)
    {
        void *ret = nullptr; // 接收线程的返回值
        int n = pthread_join(it->tid, &ret);
        assert(n == 0);
        cout << "线程退出:" << it->tid << endl;
        delete it;
    }
    cout << "主线程退出" << endl;
    return 0;
}